//密码校验
console.log("yes")
var passwordInput = document.getElementById("password");
alert(passwordInput.value);
var passwordInput2 = document.getElementById("repeatedPassword");
function checkPassword() {
    var password = passwordInput.value;
    var repeatedPassword = passwordInput2.value;
    console.log(password);
    console.log(repeatedPassword);
    var tips=document.getElementById("passwordTips");
    if(password==null||password==''||password.length==0){
        tips.className='errorTips';
        tips.innerHTML='Password cannot be empty';
        return;
    }else if(password.length<=3){
        tips.className='errorTips';
        tips.innerHTML='Password are short';
        return;
    }else if(repeatedPassword!=password){
        tips.className='errorTips';
        tips.innerHTML='Two passwords are inconsistent';
        return ;
    }else{
        tips.className='okTips';
        tips.innerHTML='Password Available';
        return;
    }
}
//后端验证
var xhr;
function checkUsername(){
    var username = document.getElementById("username").value;
    var tips = document.getElementById("usernameTips");
    //用户名不能为空
    if (username == null || username == '' || username.length == 0) {
        tips.className = 'errorTips';
        tips.textContent = "Username cannot be empty";
        return;
    }
    //用户名长度必须大于 3
    if (username.length <= 2) {
        tips.className = 'errorTips';
        tips.textContent = "Username are too short";
        return;
    }
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = fun1;
    xhr.open('GET',"usernameExist?username="+username);
    xhr.send(null);
}
//ajax验证后端
function fun1(){
    if(xhr.readyState===4){
        if(xhr.status==200){
            var tips=document.getElementById("usernameTips");
            var responseInfo = xhr.responseText.trim(); // 去掉响应内容前后的空白字符（包括换行符）
            console.log("Response: ", responseInfo);
            if(responseInfo=="Exist"){
                tips.className='errorTips';
                tips.innerHTML='Username Invalid';
            }else if(responseInfo=="Not Exist"){
                tips.className='okTips';
                tips.innerHTML='Username Available';
                return;
            }else{
                console.log('Error');
            }
        }
    }
}

